const { isLogin, request } = getApp()
import Dialog from '@vant/weapp/dialog/dialog';

Page({
  data: {
    allChecked: false,
    stepper: 1,
    items: [],
    number: 0,
    price: 0,
    del: false
  },
  // 删除商品
  delCarts() {
    Dialog.confirm({
      title: '删除',
      message: '确定要删除嘛？',
    }).then(() => {
      let checked = []
      const delChecked = []
      checked = this.data.items.filter(item => {
        return item.selected
      })
      checked.forEach(item => {
        delChecked.push(item.key)
      })
      request('/shopping-cart/remove', {
        method: 'POST',
        data: {
          key: delChecked.join(',')
        }
      }).then(res => {
        if (res.data.code === 0) {
          this.setData({
            items: res.data.data.items,
            number: res.data.data.number,
            price: res.data.data.price
          })
          wx.showToast({
            title: '删除成功',
            icon: 'none'
          })
        }
        if (res.data.code === 700) {
          this.setData({
            items: [],
            number: 0,
            price: 0
          })
        }
      })
    })
  },
  // 点击编辑显示删除
  delShow() {
    this.setData({
      del: !this.data.del
    })
  },
  // 更改商品选中状态
  fetchChecked(e) {
    request('/shopping-cart/select', {
      method: 'POST',
      data: {
        key: e.currentTarget.dataset.key,
        selected: e.detail
      }
    }).then(res => {
      if (res.data.code === 0) {
        this.setData({
          items: res.data.data.items,
          number: res.data.data.number,
          price: res.data.data.price
        })
        this.setData({
          allChecked: this.data.items.every((item) => item.selected)
        })
      }
    })
  },
  // 更改商品数量
  fetchNumber(e) {
    console.log(e);
    request('/shopping-cart/modifyNumber', {
      method: 'POST',
      data: {
        key: e.currentTarget.dataset.key,
        number: e.detail
      }
    }).then(res => {
      if (res.data.code === 0) {
        this.setData({
          items: res.data.data.items,
          number: res.data.data.number,
          price: res.data.data.price
        })
      }
    })
  },
  // 获取购物车数据
  fetchCarts() {
    request('/shopping-cart/info').then(res => {
      if (res.data.code === 0) {
        this.setData({
          items: res.data.data.items,
          number: res.data.data.number,
          price: res.data.data.price
        })
        this.setData({
          allChecked: this.data.items.every((item) => item.selected)
        })
      }
    })
  },
  // 步进器
  setOnChange(e) {
    this.setData({
      stepper: e.detail,
    });
  },
  // 全选状态
  onChange(e) {
    const allKey = []
    this.data.items.forEach(item => {
      allKey.push(item.key)
    })
    // 发送请求修改全部选中状态
    request('/shopping-cart/select', {
      method: 'POST',
      data: {
        key: allKey.join(","),
        selected: e.detail
      }
    }).then(res => {
      if (res.data.code === 0) {
        this.setData({
          items: res.data.data.items,
          number: res.data.data.number,
          price: res.data.data.price
        })
        this.setData({
          allChecked: this.data.items.every((item) => item.selected)
        })
      }
    })
  },
  onLoad(options) {
  },
  //判断是否登录
  onShow() {
    if (!isLogin()) {
      wx.navigateTo({
        url: '/pages/login/login'
      })
    }
    this.fetchCarts()
  }
})